const crypto = require('crypto');

const aesEncrypt = (data, key = 'lanaya') => {
  const cipher = crypto.createCipher('aes192', key);
  let crypted = cipher.update(data, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
};

const generateAuth = (str) => {
  return aesEncrypt(str);
};
const token = function (req, res, next) {
  let url = req.originalUrl;
  if (/^\/login/.test(url)) {
    if (req.middleData) {
      res._sendData = generateAuth(JSON.stringify(req.middleData));
    } else {
      next('error');
    }
  }
  next();
};

module.exports = token;